﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Text;
using System.Net;
using System.Security.Cryptography;

namespace MuaChung.Base
{
    public class BaseWeb
    {
        //Trạng thái đơn hàng:
        //0: chưa đặt hàng bên doanh nghiệp biz được
        //1: đã đặt hàng thành công
        //2:  
        //3: đã được doanh nghiệp confirm, đang chờ thanh toán
        //4: đã thanh toán, chờ chọn hình thức vận chuyển
        //5: đã chọn vận chuyển, đã confirm qua bên doanh nghiệp, đang chờ nhận hàng
        //6: đã nhận hàng, đơn hàng hoàn tất.
        public static string WS_BIZ5A = "BIZ5";
        public static string WS_BIZ2B = "BIZ2";
        public static string WS_BANK5D = "MSB";
        public static string WS_BANK2D = "DAB";
        public static string RootServerPath = HttpContext.Current.Server.MapPath("/");
        public static string WebLink = "" + (HttpContext.Current.Request.Url).ToString().Remove((HttpContext.Current.Request.Url).ToString().IndexOf('/',(HttpContext.Current.Request.Url).ToString().IndexOf("//")+3)+1);
        public static string BacgroundImagesPath = "/Content/Images/Backgrounds/";
        public static string CouponImagesPath = "/Content/Images/CouponImages/";
        public static string OtherImagesPath = "/Content/Images/";
        public static string SupportEmailID = "deal.hotmoingay@gmail.com";
        public static string SupportEmailPass = "hotmoingay";
        public static int PageSize = 6;
        private static readonly Random random = new Random();
        private static readonly object syncLock = new object();        

        public static bool SendMail(string senderName, string senderEmail, string pass, List<string> receiverEmail, string subject, string content)
        {
            try
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(senderEmail, senderName, System.Text.Encoding.UTF8);
                foreach (var item in receiverEmail)
                {
                    msg.To.Add(item);
                }                
                msg.Subject = subject;
                msg.Body = content;
                msg.Priority = MailPriority.High;
                msg.SubjectEncoding = Encoding.UTF8;
                msg.BodyEncoding = Encoding.UTF8;
                msg.IsBodyHtml = true;
                SmtpClient client = new SmtpClient();
                client.Credentials = new NetworkCredential(senderEmail, pass);
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.EnableSsl = true;
                client.Send(msg);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();

            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public static bool verifyMd5Hash(string input, string hash)
        {
            string hashOfInput = getMd5Hash(input);

            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private static int RandomNumber(int min, int max)
        {
            lock (syncLock)
            { // synchronize
                return random.Next(min, max);
            }
        }
        private static string RandomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                double ran;
                lock (syncLock)
                { // synchronize
                    ran = random.NextDouble();
                }
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * ran + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }
        public static string GenerateRandomCode(int strLength)
        {                        
            StringBuilder builder = new StringBuilder();            
            for (int i = 0; i < strLength; i++)
            {
                int type = RandomNumber(10, 99);
                int lower = RandomNumber(10, 99);
                if (type <= 73) //character
                {
                    if (lower <= 77)
                    {
                        builder.Append(RandomString(1, false));
                    }
                    else
                    {
                        builder.Append(RandomString(1, true));
                    }
                }
                else
                {
                    builder.Append(RandomNumber(0, 9));
                }
            }
            return builder.ToString();
        }                 
    }
}